package com.chenjun.web.controller.oa;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.swing.text.rtf.RTFEditorKit;

import org.activiti.engine.identity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.chenjun.common.Constant;
import com.chenjun.common.base.ResultDto;
import com.chenjun.common.bean.Pagination;
import com.chenjun.common.model.DataGridModel;
import com.chenjun.common.model.DataGridSortModel;
import com.chenjun.common.util.DateUtil;
import com.chenjun.common.util.ReadExcel;
import com.chenjun.core.dao.generator.entity.OaDictHolidays;
import com.chenjun.core.dao.generator.entity.OaDictSeasonCfg;
import com.chenjun.core.dao.generator.entity.OaDictYearCfg;
import com.chenjun.core.service.oa.OaDictHolidaysService;
import com.chenjun.core.service.oa.OaDictSeasonCfgService;
import com.chenjun.core.service.oa.OaDictYearCfgService;
import com.chenjun.web.controller.BaseController;

/**
 * 考情基础设置（年份 ，节假，冬夏令时）
 * @author Administrator
 *
 */
@SuppressWarnings("serial")
@Controller
@RequestMapping("bastic")
public class OaBasicSetupController extends BaseController {

	@Autowired
	private OaDictYearCfgService oaDictYearCfgService;
	@Autowired
	private OaDictHolidaysService oaDictHolidaysService;
	@Autowired
	private OaDictSeasonCfgService oaDictSeasonCfgService;
	
	/**'
	 * 设置年份
	 * @param year
	 * @param startime
	 * @param endtime
	 * @return
	 */
	@RequestMapping("/setyear")
	@ResponseBody
	public Object setyear(HttpServletRequest request,
			HttpServletResponse response,String year ,String startime ,String endtime ){
		User user=(User) request.getSession().getAttribute(Constant.Session.USER.toString());
		OaDictYearCfg oaDictYearCfg=new OaDictYearCfg();
		oaDictYearCfg.setCode("");
		oaDictYearCfg.setStatus(1);
//		oaDictYearCfg.setOperator(user.getId());
		oaDictYearCfg.setCreateTime(new Date());
		oaDictYearCfg.setYearBeginDate(DateUtil.strToDateShort(startime));
		oaDictYearCfg.setYearEndDate(DateUtil.strToDateShort(endtime));
		oaDictYearCfg.setName(year);
		int i=oaDictYearCfgService.create(oaDictYearCfg);
		ResultDto resultDto=new ResultDto();
		if(i==1){
			resultDto.setType(resultDto.SUCCESS);
			resultDto.setMessage("设置成功");
		}else{
			resultDto.setType(resultDto.ERROR);
		}
		return resultDto;
	}
	
	/**
	 * 跳转设置年份页面
	 * @return
	 */
	@RequestMapping("/setbasicyear.html")
	public ModelAndView index() {
		ModelAndView view=new ModelAndView("oabasics/oabasicsyear");
		List<OaDictYearCfg> list=oaDictYearCfgService.selectAll();
		view.addObject("yearlist", list);
		return view;
	}
	/**
	 * 跳转设置假期
	 * @return
	 */
	@RequestMapping("/setbasichoilday.html")
	public ModelAndView setbasichoilday() {
		ModelAndView view=new ModelAndView("oabasics/oabasicshoilday");
		List<OaDictYearCfg> list=oaDictYearCfgService.selectAll();
		view.addObject("yearlist", list);
		return view;
	}
	
	
	/**
	 * 获取year
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping("/findyears")
	@ResponseBody
	public Object findyears(HttpServletRequest request,
			HttpServletResponse response){
		DataGridModel dataGridModel=new DataGridModel();
		List<OaDictYearCfg> list=oaDictYearCfgService.selectAll();
		ResultDto resultDto=new ResultDto();
		resultDto.setData(list);
		return resultDto;
	}
	/**
	 * 导入 hoilday
	 * 
	 * @param request
	 * @param response
	 * @param path
	 * @return
	 */
	@RequestMapping("/inputhoilday")
	@ResponseBody
	private Object inputhoilday(HttpServletRequest request,
			HttpServletResponse response,String path){
		String upath = request.getSession().getServletContext()
				.getRealPath("/");
		String filepath=upath+path;
		try {
			List<Object[]> list=ReadExcel.readExcel(filepath);
			OaDictHolidays oaDictHolidays =new OaDictHolidays();
			oaDictHolidays.setCode("");
			oaDictHolidays.setCreateTime(new Date());
			oaDictHolidays.setYear(1111);
			oaDictHolidays.setMonth(11);
			oaDictHolidays.setName("");
			oaDictHolidays.setNote("");
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return oaDictYearCfgService;
	}
	
	/**
	 * 根据year查找hoilday
	 * 
	 * @param year
	 * @return
	 */
	@RequestMapping("/findhoilday")
	@ResponseBody
	private Object findhoilday(Integer year){
		DataGridModel param =new DataGridModel();
		Map<String, Object> map =new HashMap<String, Object>();
		map.put("year", year);
		param.setParams(map);
		param.setRows(0);
		DataGridSortModel sortBy = new DataGridSortModel("id", "ASC");
		param.getSortBy().add(sortBy);
		List<OaDictHolidays> list =oaDictHolidaysService.doListByCondition(param);
		return list;
	}
	
	
	@RequestMapping("/createsession")
	@ResponseBody
	private Object createSes(){
		OaDictSeasonCfg oaDictSeasonCfg=new OaDictSeasonCfg();
		
		
		return oaDictSeasonCfgService.create(oaDictSeasonCfg);
	}
	
	@RequestMapping("/findsession")
	@ResponseBody
	private Object findSession(){
		DataGridModel param =new DataGridModel();
		Map<String, Object> map =new HashMap<String, Object>();
		param.setParams(map);
		param.setRows(0);
		DataGridSortModel sortBy = new DataGridSortModel("id", "ASC");
		param.getSortBy().add(sortBy);
		List<OaDictSeasonCfg> list =oaDictSeasonCfgService.doListByCondition(param);
		return oaDictHolidaysService;
	}
	
	@RequestMapping("/searchholiday")
	@ResponseBody
	public ResultDto getHoliday(HttpServletRequest request, DataGridModel param) {
		ResultDto rtDto = new ResultDto();
		Pagination data = oaDictHolidaysService.doPagination(param);
		rtDto.setData(data);
		return rtDto;
	}	
	
}
